Completed
Push — master ( c218d3...33ae5d )
by Maxence
02:12
created

searchbox.initSearch   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 23
rs 9.0856
1
/*
2
 * FullTextSearch - Full text search framework for Nextcloud
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2018
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OCA */
27
/** global: nav */
28
/** global: _ */
29
/** global: api */
30
/** global: search */
31
/** global: result */
32
/** global: fullTextSearch */
33
/** global: settings */
34
35
36
var box_elements = {
37
	searchInput: null,
38
	searchMore: null,
39
	searchError: null,
40
	divFullTextSearchIcon: null,
41
	divFullTextSearchPopup: null
42
};
43
44
45
var searchbox = {
46
47
	init: function () {
48
49
		var self = this;
0 ignored issues
show
Unused Code introduced by
The variable self seems to be never used. Consider removing it.
Loading history...
50
51
		// we remove old search
52
		var search_form = $('FORM.searchbox');
53
		if (search_form.length > 0) {
54
			search_form.remove();
55
		}
56
57
58
		var divHeaderRight = $('DIV.header-right');
59
		var divFullTextSearch = $('<div>', {id: 'fulltextsearch'});
60
		divHeaderRight.prepend(divFullTextSearch);
61
62
		box_elements.divFullTextSearchIcon = searchbox.generateFullTextSearchIcon();
63
		box_elements.divFullTextSearchPopup = searchbox.generateFullTextSearchPopup();
64
		divFullTextSearch.append(box_elements.divFullTextSearchIcon);
65
		divFullTextSearch.append(box_elements.divFullTextSearchPopup);
66
67
		OC.registerMenu(box_elements.divFullTextSearchIcon, box_elements.divFullTextSearchPopup,
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
68
			searchbox.displayedSearchPopup);
69
70
		api.retrieveOptions(settings.searchProviderId);
71
72
		$(window).bind('keydown', function (event) {
73
			if (event.ctrlKey || event.metaKey) {
74
				if (String.fromCharCode(event.which).toLowerCase() === 'f') {
75
					event.preventDefault();
76
					searchbox.displaySearchPopup(true);
77
				}
78
79
				return;
80
			}
81
82
			if (event.which === 27) {
83
				searchbox.displaySearchPopup(false);
84
			}
85
		});
86
87
	},
88
89
90
	generateFullTextSearchIcon: function () {
91
		var className = 'icon-fulltextsearch';
92
		if (OCA.Theming === undefined || !OCA.Theming.inverted) {
93
			className = 'icon-fulltextsearch-white';
94
		}
95
96
		var icon = $('<div>', {
97
			id: 'fts-icon',
98
			tabindex: 0,
99
			role: 'link',
100
			class: className + ' menutoggle'
101
		});
102
103
		icon.fadeTo(0, 0.6);
104
105
		return icon;
106
	},
107
108
109
	generateFullTextSearchPopup: function () {
110
		var popup = $('<div>', {
111
			id: 'fts-popup'
112
		});
113
114
		var self = this;
115
		box_elements.searchInput = $('<input>', {
116
			id: 'fts-input',
117
			placeholder: 'Search ' + settings.searchProviderName
118
		}).on('keyup', self.searching);
119
		box_elements.searchMore = $('<div>', {id: 'fts-more'});
120
		box_elements.searchError = $('<div>', {id: 'fts-error'});
121
122
		var divHeader = $('<div>', {id: 'fts-header'});
123
		divHeader.append($('<div>').append(box_elements.searchInput));
124
125
		popup.append(divHeader);
126
		popup.append(box_elements.searchMore);
127
		popup.append(box_elements.searchError.hide());
128
129
		return popup;
130
	},
131
132
133
	displaySearchPopup: function (display) {
134
		if (display) {
135
			OC.showMenu(box_elements.divFullTextSearchIcon, box_elements.divFullTextSearchPopup,
0 ignored issues
show
Bug introduced by
The variable OC seems to be never declared. If this is a global, consider adding a /** global: OC */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
136
				searchbox.displayedSearchPopup);
137
		} else {
138
			OC.hideMenus(null);
139
		}
140
	},
141
142
143
	displayedSearchPopup: function () {
144
		box_elements.searchError.hide();
145
		box_elements.searchInput.focus();
146
	},
147
148
149
	searching: function (force) {
150
		var search = box_elements.searchInput.val();
151
		if (force === undefined) {
152
			force = false;
153
		}
154
155
		if (search.length < 1) {
156
			return;
157
		}
158
159
		if (!force && curr.lastRequest === search) {
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
160
			return;
161
		}
162
		curr.lastRequest = search;
163
		if (!searchbox.timingRequest(force)) {
164
			return;
165
		}
166
167
		api.search({
168
			providers: settings.searchProviderId,
169
			search: search,
170
			page: curr.page,
171
			options: searchbox.getSearchOptions(),
172
			size: 20
173
		});
174
	},
175
176
177
	timingRequest: function (force) {
178
		if (curr.lastRequestTimer === null) {
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
179
			curr.lastRequestTimer = window.setTimeout(function () {
180
				curr.lastRequestTimer = null;
0 ignored issues
show
Bug introduced by
The variable curr seems to be never declared. If this is a global, consider adding a /** global: curr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
181
				if (curr.lastRequestTimerQueued) {
182
					curr.lastRequestTimerQueued = false;
183
					searchbox.searching(curr.lastRequestTimerForcing);
184
				}
185
			}, settings.searchTimer);
186
		} else {
187
			curr.lastRequestTimerQueued = true;
188
			curr.lastRequestTimerForcing = force;
189
			return false;
190
		}
191
192
		return true;
193
	},
194
195
196
	onOptionsLoaded: function (result) {
197
		if (!result[settings.searchProviderId]) {
198
			return;
199
		}
200
201
		box_elements.searchMore.html(result[settings.searchProviderId]);
202
		box_elements.searchMore.find('INPUT').each(function () {
203
			$(this).on('change keyup', function () {
204
				searchbox.searching(true);
205
			});
206
		})
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
207
	},
208
209
210
	/**
211
	 *
212
	 * 0.6.0
213
	 *
214
	 *
215
	 */
216
	getSearchOptions: function () {
217
		var options = {};
218
219
		if (box_elements.searchMore === null) {
220
			return options;
221
		}
222
223
		box_elements.searchMore.find('INPUT').each(function () {
224
			var value = $(this).val();
225
226
			if ($(this).attr('type') === 'checkbox' && !$(this).is(':checked')) {
227
				value = '';
228
			}
229
230
			options[$(this).attr('id')] = value;
231
		});
232
233
		return options;
234
	}
235
236
237
};
238
239
240